﻿Public Partial Class Config_IPORT
    Inherits System.Web.UI.Page
    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "CONFIG "
            Dim ds As DataSet = Session("Config")
            txtVat.Text = ds.Tables(0).Rows(0).Item("Vat")
            txtPost.Text = ds.Tables(0).Rows(0).Item("Logon_Text")
            Editor1.Content = ds.Tables(0).Rows(0).Item("Head_Report_ComputeDraft") & ""
            Editor2.Content = ds.Tables(0).Rows(0).Item("Foot_Report_ComputeDraft") & ""
            Editor3.Content = ds.Tables(0).Rows(0).Item("Head_Report_Compute") & ""
            Editor4.Content = ds.Tables(0).Rows(0).Item("Foot_Report_Compute") & ""
            txtBefore.Text = ds.Tables(0).Rows(0).Item("Before") & ""
            txtAfter.Text = ds.Tables(0).Rows(0).Item("After") & ""
            If (Request.QueryString("P1") & "" <> "Y") Then
                btnSave.Visible = False
            End If
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (txtVat.Text.Trim() = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุ VAT !!');", True)
            Exit Sub
        End If

        If (FileUpload1.PostedFile.FileName <> "") Then
            If (System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName).ToUpper <> ".JPG") Then
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการเลือกไฟล์เป็นสกุล JPG !!');", True)
                Exit Sub
            End If
            Dim filename As String = "Baner" 'ชื่อไฟล์
            Dim Type As String = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName).ToUpper   'นามสกุล
            Dim ContentType As String = FileUpload1.PostedFile.ContentType
            Dim nameGuid As String = Guid.NewGuid().ToString()
            Try
                System.IO.File.Delete(Server.MapPath("../Image/") & "Baner.jpg")
            Catch ex As Exception

            End Try

            FileUpload1.SaveAs(Server.MapPath("../Image/") & "Baner.jpg")
        End If

        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Application.Lock()
        conn.Open()
        tran = clsDB.BeginTran(conn)
        If (clsFc.InsertConfig(txtVat.Text, CType(Session("Config"), DataSet).Tables(0).Rows(0).Item("Vehicle_Date"), Editor1.Content, Editor2.Content, Editor3.Content, Editor4.Content, txtPost.Text, txtBefore.Text, txtAfter.Text, conn, tran) < 1) Then
            condition = False
        End If
        If (condition) Then
            clsDB.CommitTran(tran)
            Dim clsGet As New ClassFunctionGetData()
            Session("Config") = clsGet.Get_Config()
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('บันทึกข้อมูลสำเร็จ');", True)
        Else
            clsDB.RollBackTran(tran)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('เกิดความผิดพลาดในการบันทึก');", True)
        End If
        Application.UnLock()
        conn.Dispose()
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub
End Class